const jwt = require('jsonwebtoken');
const secretKey = 'itheima No1 ^_^';
//model 数据层
module.exports = class admainModel extends require('./Model') {
    //查找
    static findAdmainList(req, res) {
        return new Promise((resolve, reject) => {
            let sql = 'select * from admain'
            this.query(sql)// 调用基类的query方法往里面输入sql语句
                .then((r) => {
                    resolve(r)
                })
                .catch((e) => {
                    reject(e)
                })
        })
    }
    // 查找单个信息
    static findAdmainListByCond(req, res) {
        return new Promise((resolve, reject) => {
            let { username, password } = req.body;
            let sql = 'SELECT * FROM admain WHERE username=? AND password=? AND status=1';

            this.query(sql, [username, password]) // 调用基类的query方法往里面输入sql语句
                .then((r) => {
                    resolve(r);
                })
                .catch((e) => {
                    reject(e);
                });
        });
    }


    //增加用户数据
    static addAdmain(req, res) {
        return new Promise((resolve, reject) => {
            let info = req.body
            console.log(info)
            let sql = 'insert into admain set ?'

            this.query(sql, info)
                .then((r) => {
                    res.send({ status: 1, message: 'ok', successMessage: r })
                })
                .catch((e) => {
                    res.send({ status: 0, message: 'error', errorMessage: e })
                })
        })
    }

    //删除单个数据
    static deleteUser(req, res) {
        return new Promise((resolve, reject) => {
            let { id } = req.body
            let sql = 'DELETE FROM admain WHERE id=?'
            this.query(sql, [id]) //单个id成为一个数组，思考如果id是多个组成数组，就可以实现批量删除
                .then((r) => {
                    resolve({ message: "id 为 " + id + ' 删除成功！' })
                })
                .catch((e) => {
                    reject({ message: "id 为 " + id + " 删除失败", reason: e })
                })
        })
    }

    //修改
    static changeUser(req, res) {
        return new Promise((resolve, reject) => {
            let { id, email } = req.body;
            let sql = 'UPDATE admain SET email=?, WHERE id=?';
            this.query(sql, [email, id])
                .then((r) => {
                    console.log("success", r)
                    resolve({ message: "修改成功" })
                })
                .catch((e) => {
                    console.log("error", e)
                    reject({ message: "修改失败" })
                })
        })
    }

    //禁用用户
    static disableUser(req, res) {
        return new Promise((resolve, reject) => {
            //sql 语句为 当name是xx的时候，把name修改为xxx
            let sql = 'update admain set status=0 where id=?'
            this.query(sql, [req.body.id])
                .then((r) => {
                    resolve({ message: "禁用成功" })
                })
                .catch((e) => {
                    reject({ message: "禁用失败" })
                })
        })
    }

    //获取指定用户基本信息
    static findAdmainDetail(req, res) {
        return new Promise((resolve, reject) => {
            console.log("req.headers",req.headers)

            const token = req.headers.authorization?.split(' ')[1];
            if (!token) {
                return res.status(401).send({ status: 0, message: 'error', errorMessage: "未提供授权令牌" });
            }

            try {
                const decoded = jwt.verify(token, secretKey);
                console.log('2222222',token);
                const id = decoded.id;
                console.log(id);
                let sql = 'SELECT * FROM admain WHERE id = ?';
                this.query(sql, [id])
                    .then((r) => {
                        resolve(r);
                    })
                    .catch((e) => {
                        reject(e);
                    });
            } catch (e) {
                res.status(401).send({ status: 0, message: 'error', errorMessage: "无效的授权令牌" });
                reject(e);
            }
        });
    }

}